package cn.wch.ch934xlib.connet;

import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbRequest;

import androidx.annotation.NonNull;

import java.nio.ByteBuffer;
import java.util.Locale;

import cn.wch.ch934xlib.chip.ChipType;
import cn.wch.ch934xlib.utils.FormatUtil;
import cn.wch.ch934xlib.utils.LogUtil;


public class ReadTask implements Runnable{

    private UsbEndpoint endpoint;
    private UsbDeviceConnection connection;
    private ChipType chipType;

    private boolean flag=false;
    private DataCallback dataCallback;

    private UsbRequest mRequestRead;


    public ReadTask(@NonNull ChipType chipType, @NonNull UsbDeviceConnection connection, @NonNull UsbEndpoint endpoint) {
        this.endpoint = endpoint;
        this.connection = connection;
        this.chipType = chipType;
        init();
    }

    public void init(){
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

    }

    @Override
    public void run() {
        flag=true;
        spLogUtil("读线程开始运行");
        UsbRequestPool.getInstance().init(connection,endpoint);
        while (flag){
            spLogUtil("-----read loop-----");
            UsbRequest usbRequest = connection.requestWait();
            if(usbRequest!=null){
                spLogUtil("request返回数据");
                if(!usbRequest.equals(UsbRequestPool.getInstance().getUsbRequest(endpoint))){
                    spLogUtil("该request属于其他端点");
                }
                UsbEndpoint other = usbRequest.getEndpoint();

                int endpointNumber = usbRequest.getEndpoint().getEndpointNumber();
                spLogUtil("该request属于端点:"+endpointNumber);


                ByteBuffer buffer = UsbRequestPool.getInstance().getBuffer(other);
                if(buffer==null){
                    LogUtil.d("该request的缓冲区不存在");
                    //break;
                    continue;
                }
                final int readCount = buffer.position();
                spLogUtil("readCount "+readCount);
                byte[] array = buffer.array();
                if(readCount>0){
                    spLogUtil("read value: "+ FormatUtil.bytesToHexString(array,readCount));
                    if(dataCallback!=null){
                        dataCallback.onData(other,array,readCount);
                    }
                }else if(readCount<0){
                    spLogUtil("read error: "+readCount );
                }
                UsbRequestPool.getInstance().queue(other);
            }else {
                spLogUtil("get null request");
                //break;
                continue;
            }
        }
        spLogUtil("读线程结束运行");
    }

    public void setDataCallback(DataCallback callback){
        this.dataCallback=callback;
    }

    public interface DataCallback{
        void onData(UsbEndpoint endpoint,byte[] buffer,int length);
    }

    public void stop(){
        flag=false;
        UsbRequestPool.getInstance().stop(endpoint);
    }

    public void spLogUtil(String message){
        //LogUtil.d(String.format(Locale.getDefault(),"端点%d %s",endpoint.getEndpointNumber(),message));
    }

}
